#ifndef FRAMEPRODUCERCONSUMER_H
#define FRAMEPRODUCERCONSUMER_H

#include <QWidget>
#include <QDialog>
#include <qtcore/qthread.h>
#include <qtcore/QObject>
#include <qtcore/QMutex>
#include <QtCore/QSize>
#include "GraphTools.h"
#include "FrameMaker.h"

namespace DDAGui{
    // 
    // #ifndef  PRODUCER_CONSUMER
    // #define  PRODUCER_CONSUMER
    //#define DEBUGPRODUCER

class FrameProducer: public QObject
{
    Q_OBJECT
public:
    FrameProducer(){ _dataTaker = new FrameDataTaker;}
    ~FrameProducer(){ delete _dataTaker; _dataTaker=NULL;}

Q_SIGNALS:
    void noMoreFrames();
    void oneFrameProduced();

    public Q_SLOTS:
        void produce1Frame();
        void produceFrames();
        bool setFile(char* filename){return _dataTaker->setFile(filename);}
        void setStartFrameIndex(int frameIndex);

public:
    int getTotalFramesNum(){ return _dataTaker->getSchema()[6];}
    FrameDataTaker* getFrameDataTaker() {return _dataTaker;}

private:
    void _realProduce1Frame();

private:
    FrameDataTaker* _dataTaker;
    QMutex _lock;
    QMutex _assistLock;
};

//#define DEBUGCONSUMER
class FrameConsumer: public QObject
{
    Q_OBJECT
public:
    FrameConsumer():_ifContinuePlay(true){_frameMaker = new FrameMaker;}
    ~FrameConsumer(){delete _frameMaker; _frameMaker = NULL;}

Q_SIGNALS:
    void requireFrames();
    void require1Frame();
    void frameIndexChanged(int frameIndex);
    void setStartFrameIndex(int frameIndex);

public Q_SLOTS:
    void displayFrames();
    void display1Frame(int frameIndex);
    void stopPlay();
    void startPlay();
    void pausePlay();
    void acceptPlayEnd();

public:
    void setRootSceneNode(SoSeparator* root){_frameMaker->setRootSceneNode(root); }
    FrameMaker* getFrameMaker(){return _frameMaker;}
    bool ifContinuePlay();

private:
    bool _ifContinuePlay;
    bool _ifProduceDone;
    QMutex _lock;
    QMutex _assistLock;
    FrameMaker* _frameMaker;
};
// #endif // PRODUCER_CONSUMER
// 
} //DDAGui

#endif